#!/bin/bash

arch_chroot()
{
        arch-chroot /mnt /bin/bash -c "${1}"
}

#设置源
start_mirrors()
{
	echo 'Server = https://mirrors.huaweicloud.com/archlinux/$repo/os/$arch' > /etc/pacman.d/mirrorlist
	if [ -f /$HOME/pacman.conf ]
	then
		cp -f $HOME/pacman.conf /etc
        	echo '[archlinuxcn]' >> /etc/pacman.conf
        	echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch' >> /etc/pacman.conf
        	sed -i 's/#Color/Color/g' /etc/pacman.conf
        	pacman -Syy
        	pacman -S archlinuxcn-keyring --noconfirm
        	pacman -Syy
	else
        	cp -f /etc/pacman.conf $HOME
        	echo '[archlinuxcn]' >> /etc/pacman.conf
		echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch' >> /etc/pacman.conf
        	sed -i 's/#Color/Color/g' /etc/pacman.conf
        	pacman -Syy
        	pacman -S archlinuxcn-keyring --noconfirm
        	pacman -Syy
fi
}
#开始分区
create_partitions()
{
        parted -s /dev/sda mklabel msdos
        parted -s /dev/sda mkpart primary ext4 1M 525M
        parted -s /dev/sda mkpart primary linux-swap 525M 4096M
        parted -s /dev/sda mkpart primary ext4 4096M 100%
        parted -s /dev/sda set 1 boot on
        parted -s /dev/sda print
}
#开始格式化
format_partitions()
{
        mkfs.vfat -F32 /dev/sda1
        mkswap /dev/sda2
        mkfs.ext4 /dev/sda3
}
#挂载分区
mount_partitions()
{
        mount /dev/sda3 /mnt
        swapon /dev/sda2
        mkdir /mnt/boot
        mount /dev/sda1 /mnt/boot
        lsblk
}

#最小安装
install_baseSystem()
{
	pacstrap /mnt base base-devel linux linux-firmware wqy-zenhei ttf-dejavu wqy-microhei adobe-source-code-pro-fonts --noconfirm
	pacman -Syu
	genfstab -U /mnt >> /mnt/etc/fstab
}
#创建用户
create_user()
{
	echo -e 'Do you need to create new users?   \033[31mnewuser/no\033[0m'
	read -t 6000 -p "Please enter   newuser/no   ：" newuser
	if [ ! -z $newuser ]
	then
		case "$newuser" in
			"$newuser")
				echo 'Executing Procedure*************************'
				arch_chroot "useradd -m -G wheel -s /bin/bash $newuser"
				arch_chroot "echo "$newuser:root"|chpasswd"
				;;
			"no")
				exit
				;;
			*)
				echo "You entered something wrong! Make sure you are capitalized or full/half-corner symbols! Please enter the correct letter"
				;;
		esac
	else
		echo 'Can be null!'
	fi
}
#设置安装源
over_mirrors()
{
	if [ -f /$HOME/pacman.conf ]
	then
		echo OK
	else
		echo 'Server = https://mirrors.huaweicloud.com/archlinux/$repo/os/$arch' > /mnt/etc/pacman.d/mirrorlist
		echo '[archlinuxcn]' >> /mnt/etc/pacman.conf
		echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch' >> /mnt/etc/pacman.conf
		sed -i 's/#Color/Color/g' /mnt/etc/pacman.conf
		arch_chroot "pacman -Syy"
		arch_chroot "pacman -S archlinuxcn-keyring --noconfirm"
		arch_chroot "pacman -Syy"
	fi
}

#时区和hosts明和其他设置
other_set()
{
	arch_chroot "ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime"
	arch_chroot "hwclock --systohc"
	arch_chroot "pacman -S dnsutils inetutils iproute2 vim dialog wpa_supplicant ntfs-3g networkmanager iw sudo --noconfirm"
	arch_chroot "pacman -S openssh intel-ucode bash-completion yay --noconfirm"
	arch_chroot "pacman -S ttf-dejavu wqy-zenhei wqy-microhei adobe-source-han-sans-cn-fonts net-tools openssh --noconfirm"
	arch_chroot "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config"
	arch_chroot "systemctl enable sshd"
	arch_chroot "sed -i '/zh_*/{s/#//}' /etc/locale.gen"
	arch_chroot "locale-gen"
	arch_chroot "echo LANG=zh_CN.UTF-8 > /etc/locale.conf"
	arch_chroot "echo Arch > /etc/hostname"
	arch_chroot "echo '127.0.0.1 localhost.localdomain localhost' >> /etc/hosts"
	arch_chroot "echo '::1 localhost.localdomain localhost' >> /etc/hosts"
	arch_chroot "echo '127.0.1.1 Arch.localdomain Arch' >> /etc/hosts"
}

#设置用户密码和权限
set_user()
{
	arch_chroot "systemctl enable NetworkManager.service"
	arch_chroot "echo "root:root"|chpasswd"
	arch_chroot "sed -i 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g' /etc/sudoers"
}

#安装引导
install_gurb()
{
	if [ $tmpefi == msdos ]
	then
		arch_chroot "pacman -S grub os-prober --noconfirm"
		arch_chroot "grub-install --target=i386-pc /dev/sda"
		arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg"
	else
		arch_chroot "pacman -S dosfstools grub efibootmgr --noconfirm"
		#grub-install --target=x86_64-efi --efi-directory=/boot/EFI --bootloader-id=grub
		arch_chroot "grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=boot"
		arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg"
		arch_chroot "sed -i 's/use_lvmetad = 1/use_lvmetad = 0/g' /etc/lvm/lvm.conf"
		arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg"
	fi
}
#安装桌面环境
install_desktop()
{
	echo -e 'You want to install that desktop environment?   \033[31m<1.xfce4   2.plasma(kde)   3.lxde   4.gnome   5.deepin>    no(exit)\033[0m'
	read -t 6000 -p "Please enter  1    2    3    4    5    no(exit) ：" desktop
	if [ ! -z $desktop ]
	then
		case "$desktop" in
			"1")
				echo 'Executing Procedure*************************'
				arch_chroot "pacman -S xf86-video-vesa xorg xf86-input-synaptics --noconfirm"
				arch_chroot "pacman -S alsa-utils pulseaudio pulseaudio-alsa xfce4 sddm --noconfirm"
				arch_chroot "systmectl start sddm &"
				arch_chroot "systemctl enable sddm.service"
				;;
			"2")
				echo 'Executing Procedure*************************'
				arch_chroot "pacman -S xf86-video-vesa xorg xf86-input-synaptics --noconfirm"
				arch_chroot "pacman -S alsa-utils pulseaudio pulseaudio-alsa sddm plasma --noconfirm"
				arch_chroot "systmectl start sddm &"
				arch_chroot "systemctl enable sddm.service"
				;;
			"3")
				echo 'Executing Procedure*************************'
				arch_chroot "pacman -S xf86-video-vesa xorg xf86-input-synaptics --noconfirm"
				arch_chroot "pacman -S alsa-utils pulseaudio pulseaudio-alsa lxde --noconfirm"
				arch_chroot "systemctl start lxdm.service &"
				arch_chroot "systemctl enable lxdm.service"
				;;
			"4")
				echo 'Executing Procedure*************************'
				arch_chroot "pacman -S xf86-video-vesa xorg xf86-input-synaptics --noconfirm"
				arch_chroot "pacman -S alsa-utils pulseaudio pulseaudio-alsa gnome gdm --noconfirm"
				arch_chroot "systemctl start gdm.service &"
				arch_chroot "systemctl enable gdm.service"
				;;
			"5")
				echo 'Executing Procedure*************************'
				arch_chroot "pacman -S xf86-video-vesa xorg xf86-input-synaptics --noconfirm"
				arch_chroot "pacman -S alsa-utils pulseaudio pulseaudio-alsa deepin --noconfirm"
				arch_chroot "pacman -S lightdm lightdm-deepin-greeter --noconfirm"
				arch_chroot "systemctl start lightdm.service &"
				arch_chroot "systemctl enable lightdm.service"
				;;
			"no")
				arch_chroot "pacman -S neofetch screen screenfetch wget zip unzip tar simplescreenrecorder p7zip vlc git lxterminal gedit --noconfirm"
				cat > /mnt/help.txt <<-EOF
更改家目录为英文
sudo pacman -S xdg-user-dirs-gtk
export LANG=en_US
xdg-user-dirs-gtk-update
然后会有个窗口提示语言更改，更新名称即可
export LANG=zh_CN.UTF-8
然后重启电脑如果提示语言更改，保留旧的名称即可
安装oh-my-zsh
wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh
chmod +x install.sh && ./install.sh
主题agnoster
安装谷歌浏览器：
sudo pacman -S google-chrome
安装搜狗输入法：
sudo pacman -S fcitx-sogoupinyin fcitx-configtool fcitx-im
yay -Sa fcitx-qt4
sudo vim /etc/environment 
GTK_IM_MODULE=fcitx
QT_IM_MODULE=fcitx
XMODIFIERS="@im=fcitx"
EOF
				echo -e "Congratulations, the system has been installed! User is \033[31m$newuser\033[0m and all passwords are initialized to \033[31mroot\033[0m    \033[31mumount -R /mnt\033[0m    \033[31mreboot\033[0m"
				reboot
				;;
			*)
				echo "You entered something wrong! Make sure you are capitalized or full/half-corner symbols! Please enter the correct letter"
				;;
			esac
	else
		echo 'Can be null!'
	fi
}

echo '********************************************************************************************************'
echo '*                                      Are you ready?                               *'
echo '*   This process takes a long time, please do not close the current terminal! Otherwise, the program will quit! *'
echo -e '*   Please enter "\033[31myes\033[0m" return, start installation, enter "\033[31mno\033[0m" return, exit installation, if 1 minute no exercise will automatically end the program! *'
echo '********************************************************************************************************'
#ip a | awk -F: 'NR==9{print $1}' | awk '{print $2}'
echo "root:root"|chpasswd
#启用ssh服务,显示开机启动类型
systemctl start sshd
iptpm=`ip a | grep global | awk '{print $2}' | cut -f1 -d "/"`
if [ -d /sys/firmware/efi]
then
	tmpefi=eif
else
	tmpefi=msdos
fi
echo -e "ssh: IP:\033[31m$iptmp\033[0m  USER:\033[31mroot\033[0m   PASSWD:\033[31mroot\033[0m   startup type:\033[31mr$tmpefi\033[0m"
echo 'Executing Procedure*************************'
start_mirrors
create_partitions
format_partitions
mount_partitions
install_baseSystem
over_mirrors
create_user
other_set
set_user
install_gurb
install_desktop
arch_chroot "pacman -S neofetch screen screenfetch wget zip unzip tar simplescreenrecorder p7zip vlc git lxterminal gedit --noconfirm"
cat > /mnt/help.txt <<-EOF
更改家目录为英文
sudo pacman -S xdg-user-dirs-gtk
export LANG=en_US
xdg-user-dirs-gtk-update
然后会有个窗口提示语言更改，更新名称即可
export LANG=zh_CN.UTF-8
然后重启电脑如果提示语言更改，保留旧的名称即可
安装oh-my-zsh
wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh
chmod +x install.sh && ./install.sh
主题agnoster
安装谷歌浏览器：
sudo pacman -S google-chrome
安装搜狗输入法：
sudo pacman -S fcitx-sogoupinyin fcitx-configtool fcitx-im
yay -Sa fcitx-qt4
sudo vim /etc/environment 
GTK_IM_MODULE=fcitx
QT_IM_MODULE=fcitx
XMODIFIERS="@im=fcitx"
EOF
echo -e "Congratulations, the system has been installed! User is \033[31m$newuser\033[0m and all passwords are initialized to \033[31mroot\033[0m   \033[31mumount -R /mnt\033[0m    \033[31mreboot\033[0m"
reboot
